import re
import math

def __getdate(value,date):
    pattern='(19[8-9][\d]|2000)(0[1-9]|1[0-2])(0[1-9]|[1-2][\d]|3[0-1])'
    result=re.finditer(pattern,value)
    out =1000000
    fdate = float(date)
    lastest_date=''
    if result is None:
        raise  Exception('未找到所找日期')
    else:
        try:
            a = result.__next__()
            print("满足日期格式的列表：")
            while a:
                dt=a.group()
                fdt=float(dt)
                if math.fabs(fdt-fdate)<out:
                    out=math.fabs(fdt-fdate)
                    lastest_date=dt
                print(str(a.span()[0]) + '  ' + str(dt) )
                a = result.__next__()
        except Exception:
            print('----------------')
    if out <1000000:
        print('离生日最近的日期是：'+ lastest_date )
    else:
        print('没有匹配的日期')
f=open('pi.txt')
pi=f.read()
__getdate(pi,19841203)
